﻿using System;
using System.Threading.Tasks;
using CoreCms.Net.Configuration;
using CoreCms.Net.IServices;
using CoreCms.Net.Loging;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.Model.FromBody;
using InitQ.Abstractions;
using InitQ.Attributes;
using Newtonsoft.Json;
using NLog;
using SqlSugar;

namespace CoreCms.Net.RedisMQ
{
    public class CouponDistributionSubscribe : IRedisSubscribe
    {
        private readonly ICoreCmsUserGradeServices _userGradeServices;
        private readonly ICoreCmsUserServices _userServices;
        private readonly ICoreCmsCouponServices _coreCmsCouponServices;
        private readonly ICoreCmsPromotionServices _coreCmsPromotionServices;


        /// <summary>
        /// 构造函数
        /// </summary>
        public CouponDistributionSubscribe(ICoreCmsUserGradeServices userGradeServices, ICoreCmsUserServices userServices, ICoreCmsCouponServices coreCmsCouponServices, ICoreCmsPromotionServices coreCmsPromotionServices)
        {
            _userGradeServices = userGradeServices;
            _userServices = userServices;
            _coreCmsCouponServices = coreCmsCouponServices;
            _coreCmsPromotionServices = coreCmsPromotionServices;
        }


        /// <summary>
        /// 优惠券发放给用户组队列
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        [Subscribe(RedisMessageQueueKey.CouponDistributionSubscribe)]
        private async Task CouponDistributionQueue(string msg)
        {
            try
            {
                var model = JsonConvert.DeserializeObject<FMCouponDistribution>(msg);
                if (model.id == 0)
                {
                    NLogUtil.WriteAll(NLog.LogLevel.Error, LogType.RedisMessageQueue, "优惠券发放给用户组队列", "优惠券获取失败" + msg);
                    await Task.CompletedTask;
                }

                var promotion = await _coreCmsPromotionServices.QueryByIdAsync(model.id);
                if (promotion == null)
                {
                    NLogUtil.WriteAll(NLog.LogLevel.Error, LogType.RedisMessageQueue, "优惠券发放给用户组队列", "优惠券获取失败" + msg);
                    await Task.CompletedTask;
                }

                var userGrade = _userGradeServices.QueryListByClauseAsync(p => p.id == model.userGrade);
                if (userGrade == null)
                {
                    NLogUtil.WriteAll(NLog.LogLevel.Error, LogType.RedisMessageQueue, "优惠券发放给用户组队列", "用户等级获取失败" + msg);
                    await Task.CompletedTask;
                }

                //获取用户id
                var userIds = await _userServices.GetUserIdsByGrade(model.userGrade);
                if (userIds.Count > 0)
                {
                    var successCount = 0;
                    var errorCount = 0;
                    foreach (var id in userIds)
                    {
                        var result = await _coreCmsCouponServices.AddData(id, model.id, promotion);
                        if (result.status)
                        {
                            successCount++;
                        }
                        else
                        {
                            errorCount++;
                        }
                    }
                    NLogUtil.WriteAll(NLog.LogLevel.Info, LogType.RedisMessageQueue, $"优惠券发放给用户组队列", $"成功{successCount},失败{errorCount}" + msg);
                }
                else
                {
                    NLogUtil.WriteAll(NLog.LogLevel.Info, LogType.RedisMessageQueue, "优惠券发放给用户组队列", "用户为空" + msg);
                }
            }
            catch (Exception ex)
            {
                NLogUtil.WriteAll(NLog.LogLevel.Error, LogType.RedisMessageQueue, "优惠券发放给用户组队列", msg, ex);
                throw;
            }
            await Task.CompletedTask;
        }

    }
}
